package org.baixin.ftp.control;

import javax.annotation.Generated;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.ftplet.FtpException;
import org.baixin.constant.CodeType;
import org.baixin.constant.Constant;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class FtpControl implements ServletContextAware{

	private ServletContext servletContext;
	
	
	@RequestMapping("index.htm")
	public String main(HttpServletRequest req,String param){
		System.out.println("Starting FtpServer");
		WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		FtpServer server = (FtpServer)ctx.getBean("myServer");
		String  message = checkServer(server);
		
		servletContext.setAttribute("org.apache.ftpserver", server);
		try{
			if (server==null) {
				System.out.println("FtpServer is not found!");
				message = Constant.EXCEPTION;
			}else{
				if(Constant.START.equals(param)){
					start(server);
					message = Constant.START;
				}
				if (Constant.STOP.equals(param)) {
					stop(server);
					
				}
				if (Constant.SUSPEND.equals(param)) {
					suspend(server);
					message = Constant.SUSPEND;
				}
			}
		}catch (Exception e){
			throw new RuntimeException("Failed to start FtpServer", e);
		}
		
		
		req.setAttribute("message", message);
		return "index";
	}

	private String checkServer(FtpServer server) {
		if (server.isStopped()) {
			return Constant.STOP;
		}else if (server.isSuspended()) {
			return Constant.SUSPEND;
		}
		return Constant.START;
	}

	private void suspend(FtpServer server) {
		if(server.isStopped()){
			System.out.println("FtpServer is stoped!!");
			}else if(server.isSuspended()){
				System.out.println("FtpServer is Suspended!!");
			}else{
				server.suspend();
				System.out.println("FtpServer is Suspended success!!");
			}
		
	}

	private void stop(FtpServer server) {
		if(server.isStopped()){
			System.out.println("FtpServer is stoped!!");
			}else {
				server.stop();
				System.out.println("FtpServer is stoped success!!");
			}
		}

	private void start(FtpServer server) throws FtpException {
		if(server.isStopped()){
		server.start();
		}else if(server.isSuspended()){
			server.start();
		}else{
			System.out.println("FtpServer is started!!");
		}
	}
	
	@RequestMapping(value="start.htm")
	public ModelAndView start(HttpServletRequest req){
		req.setAttribute("param", Constant.START);
		return new ModelAndView("redirect:index.htm");
	}
	
	@RequestMapping(value="stop.htm")
	public ModelAndView stop(HttpServletRequest req){
		req.setAttribute("param", Constant.STOP);
		return new ModelAndView("redirect:index.htm");
	}
	
	@RequestMapping(value="suspend.htm")
	public ModelAndView suspend(HttpServletRequest req){
		req.setAttribute("param", Constant.SUSPEND);
		return new ModelAndView("redirect:index.htm");
	}

	@Override
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}
}
